package com.cloudwise.gproject.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.cloudwise.gproject.entity.Attendance;
import com.cloudwise.gproject.dao.AttendanceDao;
import com.cloudwise.gproject.listener.DataListener;
import com.cloudwise.gproject.service.AttendanceService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.util.CollectionUtils;
import org.springframework.web.multipart.MultipartFile;

/**
 * (Attendance)表服务实现类
 *
 * @author makejava
 * @since 2021-03-25 10:25:48
 */
@Service("attendanceService")
public class AttendanceServiceImpl implements AttendanceService {
    @Resource
    private AttendanceDao attendanceDao;

    /**
     * 通过ID查询单条数据
     *
     * @param attendanceId 主键
     * @return 实例对象
     */
    @Override
    public Attendance queryById(Integer attendanceId) {
        return this.attendanceDao.queryById(attendanceId);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<Attendance> queryAllByLimit(int offset, int limit) {
        return this.attendanceDao.queryAllByLimit(offset, limit);
    }

    @Override
    public PageInfo queryAll(Integer page, Integer limit, Attendance attendance) {
        //设值查询查询
        PageHelper.startPage(page,limit);
        PageInfo PageInfo = new PageInfo(attendanceDao.queryAll(attendance));

        return PageInfo;
    }

    /**
     * 新增数据
     *
     * @param attendance 实例对象
     * @return 实例对象
     */
    @Override
    public Attendance insert(Attendance attendance) {
        this.attendanceDao.insert(attendance);
        return attendance;
    }

    /**
     * 修改数据
     *
     * @param attendance 实例对象
     * @return 实例对象
     */
    @Override
    public Attendance update(Attendance attendance) {
        this.attendanceDao.update(attendance);
        return this.queryById(attendance.getAttendanceId());
    }

    /**
     * 通过主键删除数据
     *
     * @param attendanceId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer attendanceId) {
        return this.attendanceDao.deleteById(attendanceId) > 0;
    }

    @Override
    public int deleteIds(String deleteIds) {
        return 0;
    }

    @Override
    public boolean importAttendance(MultipartFile file) {
        ExcelReader excelReader = null;
        try {
            //实例化监听对象
            DataListener dataListener =new DataListener();
            //读excel数据到excelReader对象中
            excelReader = EasyExcel.read(file.getInputStream(), Attendance.class,dataListener).build();
            ReadSheet readSheet = EasyExcel.readSheet(0).build();
            excelReader.read(readSheet);
            //定义标识符
            boolean isSuc =true;
            //获取读取到的数据列表
            List list =  dataListener.getList();
            //判断是否为空
            if(!CollectionUtils.isEmpty(list)){
                //循环对象
                for (Object o : list) {
                    Attendance attendance =(Attendance) o;
                    Attendance returnCustom = this.insert(attendance);
                    if(returnCustom==null){
                        isSuc=false;
                    }
                }
            }
            return isSuc;
        }catch (Exception e){
            e.printStackTrace();
        } finally {
            if (excelReader != null) {
                // 这里千万别忘记关闭，读的时候会创建临时文件，到时磁盘会崩的
                excelReader.finish();
            }
        }
        return false;
    }

    @Override
    public Map<String, Object> showBusinessChartByAgent(String attendanceTime) {
        Attendance attendance = new Attendance();
        attendance.setAttendanceTime(attendanceTime);
        List<Attendance> attendances = attendanceDao.queryAll(attendance);
//用来返回的新的list集合
        ArrayList<Attendance> attendancesNew = new ArrayList<>();
        //设置签到，签退时间
        int size = attendances.size();
        for (int i = 0; i < size-1; i++) {
            for (int j = i+1; j < size; j++) {
                Attendance attendance1 = attendances.get(i);
                Attendance attendance2 = attendances.get(j);
                if (attendance1.getUserId().equals(attendance2.getUserId())){
                    String[] attendance1Time = attendance1.getAttendanceTime().split(" ")[1].split(":");
                    String[] attendance2Time = attendance2.getAttendanceTime().split(" ")[1].split(":");
                    //判断小时
                    if (Integer.parseInt(attendance1Time[0])>Integer.parseInt(attendance2Time[0])){
                        //设置签到时间
                        attendance1.setCheckIn(attendance2.getAttendanceTime());
                        //设置签退时间
                        attendance1.setCheckOut(attendance1.getAttendanceTime());
                    }else if (Integer.parseInt(attendance1Time[0])==Integer.parseInt(attendance2Time[0])){
                        //判断分钟
                        if (Integer.parseInt(attendance1Time[1])>Integer.parseInt(attendance2Time[1])){
                            //设置签到时间
                            attendance1.setCheckIn(attendance2.getAttendanceTime());
                            //设置签退时间
                            attendance1.setCheckOut(attendance1.getAttendanceTime());
                        }else if(Integer.parseInt(attendance1Time[1])==Integer.parseInt(attendance2Time[1])){
                            //判断秒
                            if (Integer.parseInt(attendance1Time[2])>Integer.parseInt(attendance2Time[2])){
                                //设置签到时间
                                attendance1.setCheckIn(attendance2.getAttendanceTime());
                                //设置签退时间
                                attendance1.setCheckOut(attendance1.getAttendanceTime());
                            }else if(Integer.parseInt(attendance1Time[2])==Integer.parseInt(attendance2Time[2])){
                                //设置签到时间
                                attendance1.setCheckIn(attendance2.getAttendanceTime());
                                //设置签退时间
                                attendance1.setCheckOut(attendance1.getAttendanceTime());
                            }else {
                                //设置签到时间
                                attendance1.setCheckIn(attendance1.getAttendanceTime());
                                //设置签退时间
                                attendance1.setCheckOut(attendance2.getAttendanceTime());
                            }
                        }else {
                            //设置签到时间
                            attendance1.setCheckIn(attendance1.getAttendanceTime());
                            //设置签退时间
                            attendance1.setCheckOut(attendance2.getAttendanceTime());
                        }
                    }else {
                        //设置签到时间
                        attendance1.setCheckIn(attendance1.getAttendanceTime());
                        //设置签退时间
                        attendance1.setCheckOut(attendance2.getAttendanceTime());
                    }
                    attendancesNew.add(attendance1);
                }
            }
        }
        //设置早退，迟到状态
        for (Attendance attendance1 : attendancesNew) {
            //设置迟到状态
            if (Integer.parseInt(attendance1.getCheckIn().split(" ")[1].split(":")[0])<9){
                attendance1.setInStatus(false);
            }else {
                attendance1.setInStatus(true);
            }
            //设置早退状态
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            try {
                Date checkIn = simpleDateFormat.parse(attendance1.getCheckIn());
                Date checkOut = simpleDateFormat.parse(attendance1.getCheckOut());
                long in = checkIn.getTime();
                long out = checkOut.getTime();
                long i = out-in;
                int k = 8*60*60*1000;
                if ((i-k)>0){
                    attendance1.setOutStatus(false);
                }else {
                    attendance1.setOutStatus(true);
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

}
        return null;
    }

    @Override
    public List<String> getTimes(String startTime, String endTime) {
        return attendanceDao.getTimes(startTime,endTime);
    }

    @Override
    public List<String> getNames() {
        return attendanceDao.getNames();
    }

}
